package demo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.plaf.basic.BasicSplitPaneUI;

public class TestFrame extends JFrame{
	   private JButton enableButton;

	    private JButton unableButton;

	    private boolean canResize = true;
	    private BasicSplitPaneUI ui;

	    public TestFrame() {
	        super();
	        Container c = getContentPane();
	        final JSplitPane jsp = new JSplitPane();

	        ui = (BasicSplitPaneUI) jsp.getUI();
	        jsp.setContinuousLayout(true);
	        jsp.setLeftComponent(new JScrollPane(new JTree()));
	        jsp.setRightComponent(new JTextArea());

	        jsp.setOneTouchExpandable(true);

	        c.add(jsp);

	        JPanel p = new JPanel();
	        enableButton = new JButton("可以移动");
	        enableButton.addActionListener(new ActionListener() {

	            @Override
	            public void actionPerformed(ActionEvent e) {
	                /******************** 用这种方式 ，不要每次new *****************/
	                ((BasicSplitPaneUI) jsp.getUI()).getDivider()
	                        .setBasicSplitPaneUI(ui);
	                /*****************************************/
	                // 这种做法的问题运行一下就看到了
	                // BasicSplitPaneUI newUI = new BasicSplitPaneUI();
	                // newUI.installUI(jsp);
	                // ((BasicSplitPaneUI) jsp.getUI()).getDivider()
	                // .setBasicSplitPaneUI(newUI);
	                /*****************************************/
	            }
	        });
	        p.add(enableButton);
	        unableButton = new JButton("不可移动");
	        unableButton.addActionListener(new ActionListener() {

	            @Override
	            public void actionPerformed(ActionEvent e) {
	                ui.getDivider().setBasicSplitPaneUI(null);

	            }
	        });
	        p.add(unableButton);
	        c.add(p, BorderLayout.SOUTH);
	        setDefaultCloseOperation(EXIT_ON_CLOSE);
	        setSize(600, 400);
	        setLocationRelativeTo(null);
	        setVisible(true);
	    }

	    public static void main(String[] args) {
	        new TestFrame();
	    }
}
